package com.cory.admin.sys.service;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cory.admin.sys.domain.entity.SysResource;
import com.cory.admin.sys.mapper.SysResourceMapper;
import com.cory.boot.base.BaseService;
import com.cory.boot.domain.enumdef.EState;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 *  服务实现类
 *
 * @author xc
 * @since 2019-02-23
 */
@Service
public class SysResourceService extends BaseService<SysResourceMapper, SysResource> {
    
    public List<SysResource> sortList(SysResource param) {
        QueryWrapper<SysResource> query = new QueryWrapper<>();
        query.eq("user_type", param.getUserType()).ne("state", EState.DELETE.key).in("type", Lists.newArrayList(1, 2, 3 ,4));
        return sortList(super.list(query), 0);
    }
    
    private List<SysResource> sortList(List<SysResource> list, Integer rootId) {
        List<SysResource> sub = list.stream().filter(i -> rootId.equals(i.getPid())).collect(Collectors.toList());
        sub.sort((a, b) -> {
            return a.getOrderNum() - b.getOrderNum();
        });
        
        List<SysResource> ret = Lists.newArrayList();
        for (SysResource resource : sub) {
            ret.add(resource);
            ret.addAll(sortList(list, resource.getId()));
        }
        
        return ret;
    }
    
    @Override
    public boolean delete(Integer id) {
        List<Integer> ids = Lists.newArrayList(id);
        List<Integer> pids = Lists.newArrayList(id);
        while(pids.size() > 0) {
            QueryWrapper<SysResource> wrapper = new QueryWrapper<>();
            wrapper.ne("state", EState.DELETE.key).in("pid", pids);
            
            List<SysResource> subs = super.list(wrapper);
            pids = subs.stream().map(i -> i.getId()).collect(Collectors.toList());
            ids.addAll(pids);
        }
          
        return super.deleteByIds(ids);
    }
    
    public List<SysResource> selectUserResource(Integer userId, Integer userType) {
        return baseMapper.selectUserResource(userId, userType);
    }
    
    public List<SysResource> selectResourceByType(Integer userType) {
        QueryWrapper<SysResource> query = new QueryWrapper<>();
        query.eq("state", 1).in("user_type", userType);
        query.orderByAsc("pid").orderByAsc("order_num");
        return super.list(query);
    }
    
    public List<SysResource> selectResourceByServiceType(Integer serviceType) {
        QueryWrapper<SysResource> query = new QueryWrapper<>();
        query.eq("state", 1).eq("service_type", serviceType);
        query.orderByAsc("pid").orderByAsc("order_num");
        return super.list(query);
    }

    public SysResource getByCode(String code) {
        QueryWrapper<SysResource> query = new QueryWrapper<>();
        query.eq("code", code);

        return super.getOne(query);
    }
    
    public List<SysResource> findListByCodes(List<String> codes) {
        QueryWrapper<SysResource> query = new QueryWrapper<>();
        query.in("code", codes);
        return super.list(query);
    }
    
    public Set<Integer> selectResourcePIds(Collection<Integer> resourceIds) {
        if (resourceIds == null || resourceIds.size() == 0) {
            return Sets.newHashSet();
        }
        
        QueryWrapper<SysResource> wrapper = new QueryWrapper<>();
        wrapper.select("pid");
        wrapper.in("id", resourceIds);
        
        List<SysResource> list = super.list(wrapper);
        return list.stream().filter(i -> i.getPid() != 0).map(i -> i.getPid()).collect(Collectors.toSet());
    }
}
